package demo.swing.administrator.panel.models;

import java.util.ArrayList;
import java.util.List;

import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;

import demo.swing.Flight;

public class AdminTableModel extends AbstractTableModel implements TableModel {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 3393387419756551897L;
	final static String[] COLUMN_NAMES = {
		"Flight number",
		"Departure",
		"Destination",
		"Aeroport depart.",
		"Aeroport dest.",
		"Date",
		"Bisnes class tickets",
		"Econom class tickets",
		"Time depart.",
		"Time in flight"
	};
	List data = new ArrayList();
	public void updateData(List l){
		data = new ArrayList(l);
		fireTableDataChanged();
	}
	private String getFlightField(Object o, int nField){
		Flight f = (Flight) o;
		switch (nField) {
		case 0:
			return f.getFlightNum();
		case 1:
			return f.getDeparture();
		case 2:
			return f.getDestination();
		case 3:
			return f.getAerportDepartureId();
		case 4:
			return f.getAerportDestinationId();
		case 5:
			return f.getDate();
		case 6:
			return f.getTicketsOnBisnesClass();
		case 7:
			return f.getTicketsOnEconomClass();
		case 8:
			return f.getTimeOfDeparture();
		case 9:
			return f.getTimeInFlight();
		default:
			throw new ArrayIndexOutOfBoundsException(nField);
		}
	}
	private void setFlightField(Object o, Object value, int nField){
		Flight f = (Flight) o;
		String s = (String) value;
		switch (nField) {
		case 0:
			f.setFlightNum(s);
			break;
		case 1:
			f.setDeparture(s);
			break;
		case 2:
			f.setDestination(s);
			break;
		case 3:
			f.setAerportDepartureId(s);
			break;
		case 4:
			f.setAerportDestinationId(s);
			break;
		case 5:
			f.setDate(s);
			break;
		case 6:
			f.setTicketsOnBisnesClass(s);
			break;
		case 7:
			f.setTicketsOnEconomClass(s);
			break;
		case 8:
			f.setTimeOfDeparture(s);
			break;
		case 9:
			f.setTimeInFlight(s);
			break;
		default:
			throw new ArrayIndexOutOfBoundsException(nField);
		}
	}	
	
	@Override
	public int getRowCount() {
		// TODO Auto-generated method stub
		return data.size();
	}

	@Override
	public int getColumnCount() {
		// TODO Auto-generated method stub
		return 10;
	}

	@Override
	public String getColumnName(int columnIndex) {
		// TODO Auto-generated method stub
		return COLUMN_NAMES[columnIndex];
	}

	@Override
	public Class<?> getColumnClass(int columnIndex) {
		// TODO Auto-generated method stub
		return String.class;
	}

	@Override
	public boolean isCellEditable(int rowIndex, int columnIndex) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		// TODO Auto-generated method stub
		return getFlightField(data.get(rowIndex), columnIndex);
	}

	@Override
	public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
		// TODO Auto-generated method stub
		setFlightField(data.get(rowIndex), aValue, columnIndex);
		return;
	}

	@Override
	public void addTableModelListener(TableModelListener l) {
		// TODO Auto-generated method stub
		listenerList.add(TableModelListener.class, l);
	}

	@Override
	public void removeTableModelListener(TableModelListener l) {
		// TODO Auto-generated method stub
		listenerList.remove(TableModelListener.class, l);
	}

}
